import json, pickle, base64


class CookieSecret(object):

    # 1.加密 编码
    @classmethod
    def dumps(cls, data):
        pickle_bytes = pickle.dumps(data)
        encode_base64_bytes = base64.b64encode(pickle_bytes)
        return encode_base64_bytes.decode()

    # 2.解密 解码
    @classmethod
    def loads(cls, data):
        decode_base64_bytes = base64.b64decode(data)
        pickle_data = pickle.loads(decode_base64_bytes)
        return pickle_data


# 1.json:   将python dict/list  <====> json字符串
# carts_dict = {
#     1: {
#         2: {
#             'count': 30,
#             'selected': True
#         }
#     }
# }
# 1.1 将 dict ==> json字符串  == 1, 2本来是 int 现在 被 "1","2"
# json_carts_str = json.dumps(carts_dict)
#
# # 1.2 将 json字符串 ==> dict/list
# json_carts_dict = json.loads(json_carts_str)
#
#
# # 2.pickler: 将python的对象   <====> bytes
# # 2.1 将 obj ==> =bytes
# pickle_carts_bytes = pickle.dumps(carts_dict)
# # 2.2 将 bytes ==> obj
# pickle_carts_dict = pickle.loads(pickle_carts_bytes)


# 3.base64:  编解码
# 3.1 base64:  编码--参数 必须是二进制
# encode_base_64 = base64.b64encode(pickle_carts_bytes)
#
# # 3.2 base64:  解码--参数 可以str 也可以是 bytes
# decode_base_64 = base64.b64decode(encode_base_64.decode())
#
# print(decode_base_64)
# print(type(decode_base_64))
#
# result = pickle.loads(decode_base_64)
# print(result)
